//
//  ScoreLayer.m
//  SCRAP_ObjC
//
//  Created by Capstone Design Fall 2010 on 11/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ScoreLayer.h"
#import "Player.h"
#import "Constants.h"
#import "BackgroundLayer.h"
#import "MenuScene.h"

@implementation ScoreLayer
@synthesize playerList, scores, numberOfRounds, currentRound, size, center, menu;
@synthesize winner, sizingUp, winnerLabel, winnerScoreLabel;
@synthesize buttonString;

+(id) layerWithPlayers:(NSMutableArray*)players
{
	ScoreLayer *layer = [ScoreLayer node];
	[layer setPlayers:players];
	[layer setUpEndOfRoundScoreTable];
	[layer setUpMenu];
	
	return layer;
}

+(id) layerWithPlayers:(NSMutableArray *)players withWinner:(Player*)p
{
	ScoreLayer *layer = [ScoreLayer node];
	[layer setPlayers:players];
	layer.winner = p;
	[layer setUpEndOfGameScoreTable];
	[layer setUpMenu];
	
	return layer;
}

-(id) init
{
	if((self = [super initWithColor:ccc4(10, 10, 10, 190)]))
	{
		playerList = [[NSMutableArray alloc] initWithCapacity:4];
		numberOfRounds = 1;
		currentRound = 1;
		
		size = [[CCDirector sharedDirector] winSize];
		center = ccp(size.width / 2, size.height / 2);
		winnerLabel = nil;
		sizingUp = TRUE;
	}
	return self;
}

//setter methods
-(void) setPlayers:(NSMutableArray*)players
{
//	playerList = [[NSMutableArray alloc] initWithCapacity:4];
//	for(int i = 0; i < [players count]; i++)
//		[playerList push:[players objectAtIndex:i]];
	playerList = [players copy];
}

//build the table at end of each round
-(void) setUpScoreTable:(int) rounds
{
}

//table for whole game
//input is how many rounds, 0 for all (default)
-(void) setUpSpanOfRoundsScoreTable:(int) rounds
{
}

//table for one round
-(void) setUpEndOfRoundScoreTable
{
	buttonString = @"Continue";
	int totalPlayers = [playerList count];
	CCLabel *title = [CCLabel labelWithString:@"Scoreboard" fontName:SCRAPFont fontSize:48];
	title.position = ccp(center.x, center.y + 300);
	[self addChild:title];
	
	CCLabel *names = [CCLabel labelWithString:@"Name" fontName:SCRAPFont fontSize:38];
	CCLabel *rlabl = [CCLabel labelWithString:@"Score" fontName:SCRAPFont fontSize:38];
	CCLabel *slabl = [CCLabel labelWithString:@"Total" fontName:SCRAPFont fontSize:38];
	
	float x1 = center.x - 200;
	float x2 = center.x + 50;
	float x3 = x2 + 200;
	names.position = ccp(x1, center.y + 225);
	rlabl.position = ccp(x2, center.y + 225);
	slabl.position = ccp(x3, center.y + 225);
	
	
	[self addChild:names];
	[self addChild:rlabl];
	[self addChild:slabl];
	
	for(int i = 0; i < totalPlayers; i++)
	{
		float y = center.y + 175 - i * 75;
		Player *p = [playerList objectAtIndex:i];
		CCLabel *name = [CCLabel labelWithString:p.name fontName:SCRAPFont fontSize:36];
		name.position = ccp(x1, y);
		NSLog(@"Color: %f %f %f", p.color.r, p.color.g, p.color.b);
		name.color = p.color;
		[self addChild:name];
		
		NSString *scoreString = [NSString stringWithFormat:@"%i", p.roundScore];
		//NSString *scoreString = [NSString stringWithFormat:@"%i", p.score];
		CCLabel *score = [CCLabel labelWithString:scoreString fontName:SCRAPFont fontSize:36];
		score.position = ccp(x2, y);
		score.color = p.color;
		[self addChild:score];
		
		NSString *roundString = [NSString stringWithFormat:@"%i", p.score];
		CCLabel *rscore = [CCLabel labelWithString:roundString fontName:SCRAPFont fontSize:36];
		rscore.position = ccp(x3, y);
		rscore.color = p.color;
		[self addChild:rscore];
	}
}

-(void) sizeUp:(ccTime)dt
{
	if(!sizingUp)
	{
		sizingUp = TRUE;
		id action = [CCScaleTo actionWithDuration:.5 scale:1];
		id ease = [CCEaseIn actionWithAction:action rate:.5];
		id action2 = [CCScaleTo actionWithDuration:.5 scale:1];
		id ease2 = [CCEaseIn actionWithAction:action2 rate:.5];
		id action3 = [CCScaleTo actionWithDuration:.5 scale:1];
		id ease3 = [CCEaseIn actionWithAction:action3 rate:.5];
		[winnerLabel stopAllActions];
		[winnerScoreLabel stopAllActions];
		[winnerTotalLabel stopAllActions];
		[winnerLabel runAction:ease];
		[winnerScoreLabel runAction:ease2];
		[winnerTotalLabel runAction:ease3];
		
	}
	else
	{
		sizingUp = FALSE;
		id action = [CCScaleTo actionWithDuration:.5 scale:1.1];
		id ease = [CCEaseIn actionWithAction:action rate:.5];
		id action2 = [CCScaleTo actionWithDuration:.5 scale:1.1];
		id ease2 = [CCEaseIn actionWithAction:action2 rate:.5];
		id action3 = [CCScaleTo actionWithDuration:.5 scale:1.1];
		id ease3 = [CCEaseIn actionWithAction:action3 rate:.5];
		[winnerLabel stopAllActions];
		[winnerScoreLabel stopAllActions];
		[winnerTotalLabel stopAllActions];
		[winnerLabel runAction:ease];
		[winnerScoreLabel runAction:ease2];
		[winnerTotalLabel runAction:ease3];
	}
	
}

-(void) setUpEndOfGameScoreTable
{
	buttonString = @"Main Menu";
	int totalPlayers = [playerList count];
	CCLabel *title = [CCLabel labelWithString:@"Scoreboard" fontName:SCRAPFont fontSize:48];
	title.position = ccp(center.x, center.y + 300);
	[self addChild:title];
	
	CCLabel *names = [CCLabel labelWithString:@"Name" fontName:SCRAPFont fontSize:38];
	CCLabel *rlabl = [CCLabel labelWithString:@"Score" fontName:SCRAPFont fontSize:38];
	CCLabel *slabl = [CCLabel labelWithString:@"Total" fontName:SCRAPFont fontSize:38];
	
	float x1 = center.x - 200;
	float x2 = center.x + 50;
	float x3 = x2 + 200;
	names.position = ccp(x1, center.y + 225);
	rlabl.position = ccp(x2, center.y + 225);
	slabl.position = ccp(x3, center.y + 225);
	
	[self addChild:names];
	[self addChild:rlabl];
	[self addChild:slabl];
	
	for(int i = 0; i < totalPlayers; i++)
	{
		float y = center.y + 175 - i * 75;
		Player *p = [playerList objectAtIndex:i];
		int fontSize = 36;
		CCLabel *name = [CCLabel labelWithString:p.name fontName:SCRAPFont fontSize:fontSize];
		name.position = ccp(x1, y);
		name.color = p.color;
		[name retain];
		[self addChild:name];
		
		NSString *scoreString = [NSString stringWithFormat:@"%i", p.roundScore];
		//NSString *scoreString = [NSString stringWithFormat:@"%i", p.score];
		CCLabel *score = [CCLabel labelWithString:scoreString fontName:SCRAPFont fontSize:36];
		score.position = ccp(x2, y);
		score.color = p.color;
		[self addChild:score];
		
		NSString *roundString = [NSString stringWithFormat:@"%i", p.score];
		CCLabel *rscore = [CCLabel labelWithString:roundString fontName:SCRAPFont fontSize:36];
		rscore.position = ccp(x3, y);
		rscore.color = p.color;
		[self addChild:rscore];
		
		if(p == winner)
		{
			winnerLabel = name;
			winnerScoreLabel = score;
			winnerTotalLabel = rscore;
		}
	}
	[self schedule:@selector(sizeUp:) interval:.6];
}

-(void) continueButtonHit
{
	[BackgroundLayer FalseScoreMode];
	[self removeChild:menu cleanup:YES];
	self.position = ccp(0, -size.height * 2);
	[self unschedule:@selector(sizeUp)];
}

//menus...
-(void) setUpMenu
{
	CCSprite *continueSprite = [CCSprite spriteWithFile:@"ContinueButton.png"];
	CCSprite *continueSpriteS = [CCSprite spriteWithFile:@"ContinueButtonSelected.png"];
	if([buttonString isEqualToString:@"Main Menu"])
	{
		continueSprite = [CCSprite spriteWithFile:@"MainMenuButton.png"];
		continueSpriteS = [CCSprite spriteWithFile:@"MainMenuButtonSelected.png"];
	}
	CCMenuItemSprite *continueButton = [CCMenuItemSprite itemFromNormalSprite:continueSprite selectedSprite:continueSpriteS target:self selector:@selector(continueButtonHit)];
	continueButton.scale = 1.25;
	menu = [CCMenu menuWithItems:continueButton, nil];
	[menu alignItemsVertically];
	menu.position = ccp(center.x, center.y - 275);
	[self addChild:menu];
}

-(void) dealloc
{
	[super dealloc];
}

@end
